package com.rocker.graduation.util;

import java.security.MessageDigest;

import org.springframework.util.StringUtils;

import com.rocker.graduation.constants.Constants;

public class CipherUtils {
	
	private static final char[] hexDigits = {'0', '1', '2', '3', '4','5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F'};
	
	public static String encodeByMD5(String origin){
		String target = null;
		if (!StringUtils.isEmpty(origin)) {
			try {
				MessageDigest digest = MessageDigest.getInstance(Constants.KEY_MD5);
				byte[] result = digest.digest(origin.getBytes());
				target = byteToHex(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return target;
	}
	
	public static String byteToHex(byte[] data){
		int k = 0;
		char[] temp = new char[16 * 2];
		for (int i = 0; i < data.length; i++) {
			temp[k++] = hexDigits[data[i] >>> 4 & 0xf];
			temp[k++] = hexDigits[data[i] & 0xf];
		}
		return new String(temp);
	}
}
